ListBox stores several text items. It can interact with other controls, including Button controls. We use this control in Windows Forms. In the example program it interacts with two Buttons—through the Button Click event handler.
Drag and drop ListBox from toolbox on the window Form.

Code:
using System;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class frmListBox : Form
{
public frmListBox()
{
InitializeComponent();
}
private void frmListBoLoad(object sender, EventArgs e)
{
// Add item in ListBox
listBox1.Items.Add("C#");
listBox1.Items.Add("J#");
listBox1.Items.Add("VB");
listBox1.Items.Add("Java");
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// selected value will show in Label
label3.Text ="Selected Language is "+listBox1.SelectedItem.ToString();
}
}
}

Run The Project

When you select given option then SelectedIndexChanged event will fire and
selected Item will show in the Label.

ListBox Control Properties
Sorted: ListBox can be sorted through ListBox sorted Property
Example:
private void frmListBoLoad(object sender, EventArgs e)
{
//sorted ListBox Item
listBox1.Sorted = true;
}
ListBox Item is sorted.
BackColor: ListBox Color can be changed through ListBox BackColor Property.
Example:
private void frmListBoLoad(object sender, EventArgs e)
{
//change ListBox BackColor
listBox1.BackColor = Color.CadetBlue;
}

ListBox BackColor will be change when Application Run.
ForeColor: ListBox Fore color is changed through ListBox ForeColor Property.
Example:
private void frmListBoLoad(object sender, EventArgs e)
{
//change ListBox ForeColor
listBox1.ForeColor = Color.Red;
}

Leave Comment
2 Comments